package com.neo4j.neo4jcloud.controller;

import com.neo4j.neo4jcloud.entity.SupplyGraph;
import com.neo4j.neo4jcloud.service.SupplyGraphService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author kabuqinuo
 * @date 2020/3/26 14:17
 */
@RestController
@RequestMapping(value = "/supplyGraph")
public class SupplyGraphController {

    @Autowired
    private SupplyGraphService supplyGraphService;

    @PostMapping
    public ResponseEntity<SupplyGraph> add(HttpServletRequest request, @RequestBody SupplyGraph supplyGraph, HttpServletResponse response){
        return ResponseEntity.ok(supplyGraphService.addSupplyGraph(supplyGraph));
    }

    @GetMapping
    public ResponseEntity<SupplyGraph> findOne(HttpServletRequest request, @RequestParam(value = "id") Long id, HttpServletResponse response){
        return ResponseEntity.ok(supplyGraphService.findBySupplyGraphId(id));
    }

    @DeleteMapping
    public String deleteOne(HttpServletRequest request, @RequestParam(value = "id") Long id, HttpServletResponse response){
        supplyGraphService.deleteOneBySupplyGraphId(id);
        return "success";
    }

    @GetMapping("/findOne")
    public ResponseEntity<SupplyGraph> findOne(HttpServletRequest request, @RequestParam(value = "nodeId") String nodeId, HttpServletResponse response){
        SupplyGraph supplyGraph = supplyGraphService.findOneByNodeId(nodeId);
        return ResponseEntity.ok(supplyGraph);
    }
}
